package com.boot_reggie.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot_reggie.Service.impl.EmployeeServiceimpl;
import com.boot_reggie.common.Result;
import com.boot_reggie.domain.Employee;
import com.boot_reggie.filter.adminTest;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

//@CrossOrigin(value = "http://localhost:8080")
@Slf4j
@RestController
@RequestMapping("/admin")
public class UserController {
    @Autowired
    private EmployeeServiceimpl employeeServiceimpl;
    @PostMapping("save")
    public Result<String> save(HttpServletRequest req, @RequestBody Employee employee){

        //            判断是否需要监控的地址
        if(adminTest.test(req.getRequestURI())){
            String adminurl = String.valueOf(req.getSession().getAttribute("employee"));
            if(!adminurl.equals("1")){
                return Result.kwone();
            }
        };
        Long data = (Long)req.getSession().getAttribute("employee");
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setStatus(1);
        boolean num = employeeServiceimpl.save(employee);
        if(num){
            return Result.success("数据添加成功");
        }
        return Result.error("接口异常");
    }
    @PostMapping("login")
    public Result<Employee> login(HttpServletRequest req, @RequestBody Employee employee){

//        将页面提交的密码进行md5加密
        String pwd  = employee.getPassword();

        pwd = DigestUtils.md5DigestAsHex(pwd.getBytes());

//        根据提交的用户名进行数据库匹配
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeServiceimpl.getOne(lambdaQueryWrapper);
//        System.out.println(emp);
//        判断用户是否存在
        if(emp == null){
            return Result.error("登录失败");
        }
//        判断是否密码正确
        if(!emp.getPassword().equals(pwd)){
            return Result.error("密码不正确");
        }
//        判断账户是否被禁用
        if(emp.getStatus() == 0){
            return Result.error("账户已禁用");
        }
//        将账户信息存进session返回

        req.getSession().setAttribute("employee",emp.getId());

//        将数据封装进Result.data中
        return Result.success(emp,"登录成功");
    }
//    退出功能
    @DeleteMapping("login")
    public Result<String> logout(HttpServletRequest req){
        System.out.println("===================");
        req.getSession().setAttribute("employee",null);
        return Result.success("退出成功");

    }
//    检测有无登录
    @GetMapping("test")
    public Result<String> test(HttpServletRequest req){
        Long data = (Long) req.getSession().getAttribute("employee");
        if(data == null){
            return Result.error("未登录");
        }
        return Result.success("已登陆");
    }
//    分页查询
    @GetMapping("getAll")
    public Result<IPage> getAll(HttpServletRequest req,int page,int size,String name){

        IPage page1 = new Page(page,size);
        page1 = employeeServiceimpl.getPage(page1, name);
        if(page > page1.getPages()){
          page1 = new Page(page1.getPages(),size);
          page1 = employeeServiceimpl.getPage(page1,name);
          return Result.pageData(page1);
        };
        System.out.println(page1.getRecords());
        return Result.pageData(page1);
    }
//    修改功能
    @PutMapping("modify")
    public Result<String> modify(HttpServletRequest request ,@RequestBody Employee employee){
        //            判断是否需要监控的地址
        if(adminTest.test(request.getRequestURI())){
            String adminurl = String.valueOf(request.getSession().getAttribute("employee"));
            if(!adminurl.equals("1")){
                return Result.kwone();
            }
        }
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        boolean data = employeeServiceimpl.updateById(employee);
        if(data){
            return Result.success("修改成功");
        }
        return Result.success("修改失败");
    }
//    删除功能
    @DeleteMapping("delete")
    public Result<String> delete(HttpServletRequest req,@RequestBody Employee employee){
        //            判断是否需要监控的地址
        if(adminTest.test(req.getRequestURI())){
            String adminurl = String.valueOf(req.getSession().getAttribute("employee"));
            if(!adminurl.equals("1")){
                return Result.kwone();
            }
        }
        boolean data = employeeServiceimpl.removeById(employee.getId());
        if(data){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
}
